package com.tarena.lbs.user.service.impl;


import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    AdminRepository  adminRepository;
    @Autowired
    UserRepository  userRepository;
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;


    @Override
    public Result<LoginVO> adminLogin(AdminLoginParam param) {
        if (param == null || param.getPhone() == null || param.getPassword() == null) {
            return Result.fail(PassportResultEnum.ADMIN_NONE_EXIST.getCode(),
                    PassportResultEnum.ADMIN_NONE_EXIST.getMessage());
        }
        log.info("管理员登录：{}", param);
        AdminPO adminPO= adminRepository.adminLogin(param.getPhone());
        if(adminPO==null){
            return Result.fail(PassportResultEnum.ADMIN_NONE_EXIST.getCode(),
                    PassportResultEnum.ADMIN_NONE_EXIST.getMessage());
        }
        if(!adminPO.getAccountPassword().equals(param.getPassword())){
            return Result.fail(PassportResultEnum.ADMIN_PASSWORD_ERROR.getCode(),
                    PassportResultEnum.ADMIN_PASSWORD_ERROR.getMessage());
        }else {
            try {
                log.debug("查询到的用户信息：{}", adminPO);
                Roles role = adminPO.getAccountType() == 0 ? Roles.ADMIN : Roles.SHOP;
                String jwt = generateJwt(adminPO.getId(), adminPO.getNickname(), role);
                LoginVO loginVO = new LoginVO();
                loginVO.setJwt(jwt);
                loginVO.setId(adminPO.getId());
                loginVO.setNickname(adminPO.getNickname());
                return new Result<>(loginVO);

            } catch (BusinessException e) {
                return Result.fail(e.getCode(), e.getMessage());
            }
        }
    }
    //方法考虑复用
    private String generateJwt(Integer id,String nickname,Roles role) throws BusinessException {
        UserPrinciple userPrinciple=new UserPrinciple();
        userPrinciple.setId(id);
        userPrinciple.setNickname(nickname);
        userPrinciple.setRole(role);
        return jwtEncoder.generateToken(userPrinciple);
    }

    @Override
    public Result<LoginVO> doUserLogin(UserLoginParam param) {
        if (param == null || param.getPhone() == null || param.getPassword() == null) {
            return Result.fail(PassportResultEnum.ADMIN_NONE_EXIST.getCode(),
                    PassportResultEnum.ADMIN_NONE_EXIST.getMessage());
        }
        log.info("用户登录：{}", param);
        UserPO userPO = userRepository.userLogin(param.getPhone());
        if (userPO == null) {
            return Result.fail(PassportResultEnum.ADMIN_NONE_EXIST.getCode(),
                    PassportResultEnum.ADMIN_NONE_EXIST.getMessage());
        }
        if (!userPO.getPassword().equals(param.getPassword())) {
            return Result.fail(PassportResultEnum.ADMIN_PASSWORD_ERROR.getCode(),
                    PassportResultEnum.ADMIN_PASSWORD_ERROR.getMessage());
        } else {
            log.debug("查询到的用户信息：{}", userPO);
            LoginVO loginVO = new LoginVO();
            loginVO.setId(userPO.getId());
            loginVO.setNickname(userPO.getNickName());
            return new Result<>(loginVO);
        }
    }
}
